package com.xianbeiying.app.ckgl.photo.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.activity.BaseActivity;
import com.xianbeiying.app.ckgl.photo.fragment.ImagePagerFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片预览
 */
public class PhotoPagerActivity extends BaseActivity {
    //标题
    @ViewInject(R.id.tv_title)
    private TextView tv_title;
    //返回
    @ViewInject(R.id.ly_title_left)
    private LinearLayout ly_title_left;
    //右边
    @ViewInject(R.id.iv_right)
    private ImageView iv_right;
    @ViewInject(R.id.ly_title_right)
    private LinearLayout ly_title_right;
    private ImagePagerFragment pagerFragment;
    public final static String EXTRA_CURRENT_ITEM = "current_item";
    public final static String EXTRA_PHOTOS = "photos";
    int father_sun, father;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_photo_pager);
        ViewUtils.inject(this);
        init();
        int currentItem = getIntent().getIntExtra(EXTRA_CURRENT_ITEM, 0);
        father_sun = this.getIntent().getIntExtra("FATHER_SUN", -1);
        LogUtils.e("-----FATHER_SUN==" + father_sun);
        father = this.getIntent().getIntExtra("FATHER", -1);
        LogUtils.e("-----FATHER" + father);
        List<String> paths = getIntent().getStringArrayListExtra(EXTRA_PHOTOS);
        pagerFragment =
                (ImagePagerFragment) getSupportFragmentManager().findFragmentById(R.id.photoPagerFragment);
        pagerFragment.setPhotos(paths, currentItem);
        pagerFragment.getViewPager().addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int i) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    private void init() {
        tv_title.setText("图片预览");
        iv_right.setVisibility(View.VISIBLE);
        iv_right.setImageResource(R.mipmap.gray_delete);
        //返回
        ly_title_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        //删除
        ly_title_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delete();
            }
        });
    }

    //返回
    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS, pagerFragment.getPaths());
        intent.putExtra("FATHER_SUN", father_sun);
        intent.putExtra("FATHER", father);
        setResult(RESULT_OK, intent);
        finish();
        super.onBackPressed();
    }

    //删除
    private void delete() {
        final int index = pagerFragment.getCurrentItem();

        final String deletedPath = pagerFragment.getPaths().get(index);

//        Snackbar snackbar = Snackbar.make(pagerFragment.getView(), R.string.deleted_a_photo,
//                Snackbar.LENGTH_LONG);

        if (pagerFragment.getPaths().size() <= 1) {

            // show confirm dialog
            new AlertDialog.Builder(this)
                    .setTitle(R.string.confirm_to_delete)
                    .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                            Intent intent = new Intent();
                            intent.putExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS, new ArrayList<String>());
                            intent.putExtra("FATHER_SUN", father_sun);
                            intent.putExtra("FATHER", father);
                            setResult(RESULT_OK, intent);
                            finish();
                        }
                    })
                    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    })
                    .show();

        } else {

//            snackbar.show();

            pagerFragment.getPaths().remove(index);
            //pagerFragment.getViewPager().removeViewAt(index);
            pagerFragment.getViewPager().getAdapter().notifyDataSetChanged();
        }

//        snackbar.setAction(R.string.undo, new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (pagerFragment.getPaths().size() > 0) {
//                    pagerFragment.getPaths().add(index, deletedPath);
//                } else {
//                    pagerFragment.getPaths().add(deletedPath);
//                }
//                pagerFragment.getViewPager().getAdapter().notifyDataSetChanged();
//                pagerFragment.getViewPager().setCurrentItem(index, true);
//            }
//        });
    }
}
